﻿Public Class frmParticularidade
    Public codSala As Integer = 0
    Public salSala As String = ""
    Public DesSala As String = ""
    Public tipo, ret As String
    Public codigo As Integer
    Dim objBLL As New BLLPaSala
    Dim DiaSemana As Integer = Date.Now.DayOfWeek
#Region "Load"
    Private Sub frmParticularidade_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbSala.DropDownHeight = vbNull
        objconAgenda.preencheCombo(cbDiaSemana, "age_diasemana", "dia_codigo", "dia_descricao", "", "dia_codigo")
        cbDiaSemana.SelectedIndex = DiaSemana
        Limpa()
        Select Case tipo
            Case "Novo"
                tsbAdicionar.Visible = True
                tsbAlterar.Visible = False
                lbTitulo.Text = "Particularidade da Sala"
                lbCodigo.Text = 0
                lbCodigo.Visible = False
            Case "Alterar"
                tsbAlterar.Visible = True
                tsbAdicionar.Visible = False
                CarregaCampos(codigo)
            Case Else

        End Select
    End Sub
#End Region
#Region "Métodos"
    Sub CarregaCampos(ByVal codigo As Integer)
        Dim tab As DataTable
        tab = objBLL.LocalizaParticularidade(0, codigo)
        lbCodigo.Visible = True
        lbTitulo.Text = "Part. " & tab.Rows(0)("sal_descricao").ToString()
        lbCodigo.Text = "Código: " & codigo
        codSala = tab.Rows(0)("sal_codigo")
        cbSala.Text = tab.Rows(0)("sal_sala").ToString()
        txtDescricao.Text = tab.Rows(0)("sal_descricao").ToString()
        cbDiaSemana.SelectedIndex = (tab.Rows(0)("dia_codigo") - 1)
        dtpInicio.Text = tab.Rows(0)("par_horainicio").ToString()
        dtpFim.Text = tab.Rows(0)("par_horafinal").ToString()
        rtbDetalhes.Text = tab.Rows(0)("par_detalhes").ToString()

    End Sub
    Sub CarregaPropriedades()
        objBLL.pcodigo = codigo
        objBLL.pcodsala = codSala
        objBLL.pcoddia = cbDiaSemana.SelectedValue
        objBLL.phinicio = FormatDateTime(dtpInicio.Text, vbShortTime)
        objBLL.phfinal = FormatDateTime(dtpFim.Text, vbShortTime)
        objBLL.pdescricao = rtbDetalhes.Text
    End Sub
    Sub Atualiza()
        fMenu.TPRelacionamento.Controls.Clear()
        Dim uc = New ucCadastroParticularidade()
        uc.Dock = DockStyle.Fill
        fMenu.TPRelacionamento.Controls.Add(uc)
        uc = Nothing
    End Sub
    Sub Limpa()
        cbSala.Text = ""
        cbDiaSemana.SelectedIndex = DiaSemana
        txtDescricao.Text = ""
        rtbDetalhes.Text = ""
    End Sub
#End Region
#Region "ComboBox Sala"
    Private Sub cbSala_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbSala.DropDown
        frmConsultaSala.ret = "S"
        frmConsultaSala.ShowDialog()
        If ret = "S" Then
            cbSala.Text = salSala
            txtDescricao.Text = DesSala
            cbDiaSemana.Focus()
        Else
            cbSala.Text = ""
            txtDescricao.Text = ""
        End If

    End Sub

    Private Sub cbSala_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbSala.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            e.Handled = True
            frmConsultaSala.Pesquisa = cbSala.Text
            cbSala_DropDown(sender, e)
        Else
            txtDescricao.Text = ""
        End If

    End Sub
#End Region
#Region "ComboBox Dia da Semana"
    Private Sub cbDiaSemana_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbDiaSemana.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "Botão Adicionar"
    Private Sub tsbAdicionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAdicionar.Click
        Try
            Dim ret As Integer
            If codSala <> 0 And cbDiaSemana.SelectedIndex <> -1 Then
                CarregaPropriedades()
                ret = objBLL.inserir()
                Select Case ret
                    Case 0
                        MsgBox("Particularidade gravada com sucesso", MsgBoxStyle.OkOnly)
                        Atualiza()
                        Limpa()
                    Case 1
                        MsgBox("Hora início maior/igual hora final", MsgBoxStyle.Information)
                    Case 2
                        MsgBox("Conflito entre particularidades", MsgBoxStyle.Information)
                    Case Else
                        MsgBox("Erro!!", MsgBoxStyle.Critical)
                End Select
            Else
                MsgBox("Preencher campos obrigatórios", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub
#End Region

    Private Sub tsbVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbVoltar.Click
        Me.Close()
    End Sub
#Region "Alterar"
    Private Sub tsbAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAlterar.Click
        Try
            Dim ret As Integer
            If codSala <> 0 And cbDiaSemana.SelectedIndex <> -1 Then
                CarregaPropriedades()
                ret = objBLL.Alterar()
                Select Case ret
                    Case 0
                        MsgBox("Particularidade do " & txtDescricao.Text & " alterada com sucesso", MsgBoxStyle.OkOnly)
                        Atualiza()
                        Limpa()
                        tsbVoltar_Click(sender, e)
                    Case 1
                        MsgBox("Hora início maior/igual hora final", MsgBoxStyle.Information)
                    Case 2
                        MsgBox("Conflito entre particularidades", MsgBoxStyle.Information)
                    Case Else
                        MsgBox("Erro!!", MsgBoxStyle.Critical)
                End Select
            Else
                MsgBox("Preencher campos obrigatórios", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

#End Region
End Class